﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FPModel;

namespace FacebookPoster
{
    public class User
    {
        public static List<FBUser> Get()
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBUsers.ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static FBUser GetByID(string userid)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBUsers.FirstOrDefault(us => us.UserID == userid);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

        }

        public static FBUser GetByUserToken(string userToken)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBUsers.FirstOrDefault(us => us.UserToken.ToLower() == userToken.ToLower());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static int Add(FBUser user)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    context.AddToFBUsers(user);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Update(int id, string newName)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBUser userEdit = new FBUser();
                    userEdit = (from c in context.FBUsers where c.ID == id select c).First();
                    userEdit.FirstName = newName;
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Delete(int id)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBUser catEdit = new FBUser();
                    catEdit = (from c in context.FBUsers where c.ID == id select c).First();

                    context.DeleteObject(catEdit);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }


    }
}